// pages/my/vipOrder/vipOrderList.js
  const app = getApp()
Page({
  data: {
    vipUserName:'',
    goodsInfoShow:true,
    goodsPriceShow:true,
    blackPrice:true,
    actualPriceShow:true,
    orderType: 0,
    orderTab:0,
    orders:[],
    newGoodsList:[],
    oldGoodsList:[],
    orderPrice:[],
    sales_man:[],
    orderNo:[],
    orderIds:[],
  },
  onLoad: function (options) {
    app.getMemInfo().then(res=>{
      console.log(res)
      this.setData({
        vipUserName:res.data.vipUserName
      })
      this.getOrderList(res.data.vipUserName)
      this.getOldOrderList(res.data.vipUserName)
    })
  },
  onShow: function () {

  },
  changeType(e){
    let status = e.currentTarget.dataset.paystatus
    this.setData({
      orderTab:status
    })
  },
  getOrderList(agentAdminName){
    wx.showLoading({
      title:'正在加载'
    })
    let data = {
      agentAdminName,
      orderPayState:2,
      isOldForNew:0
    }
    console.log(data)
    app.request('wx_app/user_order/bossList',data,'GET').then(res=>{
      console.log(res)
      if (res.code==1) {
        if (Object.keys(res.data).length==0) {
          app.errorMsg('未查询到订单')
          this.setData({
            orders:[]
          })
        }else{
          let orders = []
          res.data.orders.forEach((item,i)=>{
            let goodsList = []
            let total = 0   
            item.orderDetails.forEach(res=>{
              goodsList.push({
                name:res.name,
                num:res.amount,
                url:res.pic,
                type:res.des,
                price:res.price,
                actualPrice:res.actualPrice,
                companyName:res.productTag,
                color:res.tagColor,
                isDeleted:res.isDeleted
              })
              total += parseInt(res.amount)
            })
            orders.push({
              totalNumber:item.nums,
              orderNo:item.orderNo,
              sales_man:item.sales_man,
              orderPrice:item.orderPrice,
              goodsList:goodsList,
              orderId:item.orderId,
              total
            })
          })  
          this.setData({
            orders,
            actualPriceShow:true
          })
          wx.hideLoading()
        }
      }else{
        app.errorMsg(res.message)
      }
    })
  },
  getOldOrderList(agentAdminName){
    wx.showLoading({
      title:'正在加载'
    })
    let that = this
    let data = {
      agentAdminName,
      orderPayState:2,
      isOldForNew:1
    }
    console.log(data)
    app.request('wx_app/user_order/bossList',data,'GET').then(res=>{
      console.log(res)
      if (res.code==1) {
        let newGoodsList = [],
            oldGoodsList = [],
            orderPrice = [],
            sales_man = [],
            orderNo = [],
            orderIds = []
        if (res.data.orders) {
          res.data.orders.forEach(item=>{
            let temporary1 = []
            let temporary2 = []
            //获取新商品列表
            item.orderDetails.forEach(_item=>{
              temporary1.push({
                name:_item.name,
                num:_item.amount,
                actualPrice:_item.actualPrice,
                price:_item.amount,
                type:_item.des,
                url:_item.pic,
                id:_item.productId,
                color:_item.tagColor,
                companyName:_item.productTag
              })
            })
            //获取旧商品列表
            item.oldSkus.forEach(_item=>{
              temporary2.push({
                name:_item.productName,
                url:_item.imgUrl1,
                num:_item.amount,
                price:_item.stockPrice,
                type:_item.versionValues,
                id:_item.id,
                color:_item.tagColor,
                companyName:_item.productTag
              })
            })
            newGoodsList.push(temporary1)
            oldGoodsList.push(temporary2)
            orderPrice.push(item.orderPrice)
            sales_man.push(item.sales_man)
            orderNo.push(item.orderNo)
            orderIds.push(item.orderId)
          })
        }else{
          wx.hideLoading()
          app.errorMsg('没有订单')
        }
        that.setData({
          newGoodsList,
          oldGoodsList,
          orderPrice,
          sales_man,
          orderNo,
          orderIds
        })
        wx.hideLoading()
      }else{
        wx.hideLoading()
        app.errorMsg(res.message)
      }
    })
  },
  viewOrderDetailTap(e){
    let orderId = e.currentTarget.dataset.orderid
    wx.navigateTo({
      url:'vipOrderDetail?orderId='+orderId
    })
  },
  getOrderDetail(e){
    let orderId = e.currentTarget.dataset.id
    wx.navigateTo({
      url:'vipReplaceDetail?orderId='+orderId
    })
  }
})